package fr.capsuleaube.android.fragment;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import java.net.URL;
import java.util.ArrayList;

import fr.capsuleaube.android.R;
import fr.capsuleaube.android.utils.ImageLoader;

public class CapsulePhotoAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;
    private ImageLoader imageLoader;
    private ArrayList<URL> photoURLArrayList = new ArrayList<URL>();

    public CapsulePhotoAdapter(Activity a) {
        inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Create ImageLoader object to download and show image in list
        // Call ImageLoader constructor to initialize FileCache
        imageLoader = new ImageLoader(a.getApplicationContext());
    }

    public int getCount() {
        return photoURLArrayList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        ViewSwitcher lViewSwitcher;

        if (convertView == null) {

            vi = inflater.inflate(R.layout.capsulephoto_viewswitcher_element, null);
            lViewSwitcher = (ViewSwitcher) vi.findViewById(R.id.viewswitcher);

            /************  Set holder with LayoutInflater ************/
            vi.setTag(lViewSwitcher);

        } else {
            lViewSwitcher = (ViewSwitcher) vi.getTag();
        }

        ImageView lImageView = (ImageView) lViewSwitcher.getChildAt(1);
        lViewSwitcher.setDisplayedChild(0);

        imageLoader.DisplayImage(photoURLArrayList.get(position).toString(), lImageView, lViewSwitcher);

        return vi;
    }

    public void addListItems(ArrayList<URL> list) {
        photoURLArrayList.addAll(list);
    }
}